package com.listeningframework.boot.commons.httpclient;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;

import com.listeningframework.boot.commons.http.HttpMethod;
import com.listeningframework.boot.constants.Encoding;

/**
 * 
 * HttpClient MethodBuilder
 *
 * @author lixining
 * @version $Id: MethodBuilder.java, v 0.1 2016年8月16日 下午4:33:22 lixining Exp $
 */
public class HttpClinetMethodBuilder {
    /**
     * 请求地址
     */
    private String     address;
    /**
     * 请求参数
     */
    private HttpEntity httpEntity;
    /**
     * 请求方式
     */
    private HttpMethod httpMethod;
    /**
     * 编码方式,默认utf-8
     */
    private Encoding   encoding       = Encoding.UTF8;
    /**
     * Connect Timeout
     */
    private Integer    connectTimeout = 30000;
    /**
     * Socket Timeout
     */
    private Integer    socketTimeout  = 60000;

    private HttpClinetMethodBuilder() {

    }

    public static HttpClinetMethodBuilder newinstance() {
        return new HttpClinetMethodBuilder();
    }

    public HttpClinetMethodBuilder withAddress(String address) {
        this.address = address;
        return this;
    }

    public HttpClinetMethodBuilder withHttpEntity(HttpEntity httpEntity) {
        this.httpEntity = httpEntity;
        return this;
    }

    public HttpClinetMethodBuilder withHttpMethod(HttpMethod httpMethod) {
        this.httpMethod = httpMethod;
        return this;
    }

    public HttpClinetMethodBuilder withConnectTimeout(Integer connectTimeout) {
        this.connectTimeout = connectTimeout;
        return this;
    }

    public HttpClinetMethodBuilder withSocketTimeout(Integer socketTimeout) {
        this.socketTimeout = socketTimeout;
        return this;
    }

    public HttpClientMethod build() {
        HttpClientMethod method = new HttpClientMethod();
        if (StringUtils.isNotBlank(address)) {
            method.setAddress(address);
        }
        if (httpEntity != null) {
            method.setHttpEntity(httpEntity);
        }
        if (httpMethod != null) {
            method.setHttpMethod(httpMethod);
        }
        method.setEncoding(encoding == null ? Encoding.UTF8 : encoding);
        if (socketTimeout != null) {
            method.setSocketTimeout(socketTimeout);
        }
        if (connectTimeout != null) {
            method.setConnectTimeout(connectTimeout);
        }
        return method;
    }
}
